#!/bin/bash

echo "Love from SHIN MEGAMI TENSEI IV"
echo "This script is written by FLYING_PIGEON."
echo "Replacing the names..."
sh replace.sh
echo "Replace Completed."
sleep 0.3s
# 备份目录A下的所有文件到/home/pigeon/copies/
cp -r A/* /home/pigeon/copies/ | pv -s $(du -sb A/ | awk '{print $1}')

# 为A目录及其递归目录下的所有文件中的.xml文件执行python change.py
find A -type f -name "*.xml" -exec python change.py {} \; | pv -l -s $(find A -type f -name "*.xml" | wc -l)
echo "Change Completed."
sleep 0.3s
# 删除A目录及其递归目录下的所有文件中的.xml文件
find A -type f -name "*.xml" -exec rm {} \; | pv -l -s $(find A -type f -name "*.xml" | wc -l)

# 将A目录及其递归目录下的所有文件中的.xml.new文件的后缀更改为.xml
find A -type f -name "*.xml.new" -exec sh -c 'mv "$1" "${1%.new}"' sh {} \; | pv -l -s $(find A -type f -name "*.xml.new" | wc -l)

# 为A目录及其递归目录下的所有文件中的.xml文件执行python find.py，并显示输出
find A -type f -name "*.xml" -exec python find.py {} \; | pv -l -s $(find A -type f -name "*.xml" | wc -l)
echo "Please don't worry,the information above sometimes makes mistakes."
sleep 0.3s
# 判断输出是否只包含 "all perfect" 字符串，如果不是，则终止工作流
output=$(find A -type f -name "*.xml" -exec python find.py {} \;)
if [[ "$output" != *"all perfect"* ]]; then

  cp -r /home/pigeon/copies/* A/
  exit 1
fi

find A -type f -name "*.mbm" -exec sh -c 'mbm_file="$1"; xml_file="${mbm_file%.mbm}.xml"; echo "Processing: $mbm_file"; python mbm2.py import "$mbm_file" "$xml_file"' sh {} \; | pv -l -s $(find A -type f -name "*.mbm" | wc -l)
echo "Import Completed."
sleep 0.5s

